<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('项目关键条款') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('基金名称')" prop="fundId">
        <el-select v-model="queryParams.fundId" :placeholder="$t('基金')" clearable filterable>
          <el-option
            v-for="dict in fundBaseList"
            :key="dict.id"
            :label="$t(dict.fundName)"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('项目名称')" prop="dealName">
        <el-input
          v-model="queryParams.dealName"
          :placeholder="$t('项目名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('负责人')" prop="personCharge">
        <el-input
          v-model="queryParams.personCharge"
          :placeholder="$t('负责人')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('合同名称')" prop="associatedName">
        <el-input
          v-model="queryParams.associatedName"
          :placeholder="$t('合同名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('条款类型')" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          :placeholder="$t('条款类型')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('提醒人')" prop="userName">
        <el-input
          v-model="queryParams.userName"
          :placeholder="$t('提醒人')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('提醒时间')" prop="remindTime">
        <DatePicker v-model="queryParams.remindTime" value-format="yyyy-MM-dd hh:mm:ss" :placeholder="$t('提醒时间')" />
      </el-form-item>
      <el-form-item :label="$t('触发状态')" prop="isTriggerName">
        <el-select v-model="queryParams.isTrigger" :placeholder="$t('触发状态')" clearable>
          <el-option
            v-for="dict in istriggernameOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        <el-button
          v-hasPermi="['fund:overview:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="termsOverviewList"
      tooltip-effect="darkTable"
      :span-method="spanMethod"
      border
      class="noneHoverBg"
    >
      <el-table-column :label="$t('项目名称')" align="center" prop="dealName" fixed="left" show-overflow-tooltip min-width="200">
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{path:'/deal/home/dealBaseHome',query:{id:scope.row.formDealId, companyId: scope.row.companyId}}"
            class="link_a"
          >{{ scope.row.dealName }}</router-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('负责人')" align="center" prop="personCharge" fixed="left" width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.personCharge }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('合同名称')" align="center" prop="associatedName" show-overflow-tooltip width="200">
        <template slot-scope="scope">
          <span class="link-type" @click="viewContractProcess(scope.row)">{{ scope.row.associatedName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('条款类型')" align="center" prop="typeName" width="200" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="link-type" @click="viewTerms(scope.row)">{{ $t(scope.row.typeName) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('提醒人')" align="center" prop="userName" width="130" show-overflow-tooltip>
        <template slot="header" slot-scope="{}">
          <span>{{ $t('提醒人') }}</span>
          <el-tooltip :content="$t('提醒人中已包含默认提醒人')" placement="top">
            <i class="el-icon-question" />
          </el-tooltip>
        </template>
        <template #default="scope">
          <span>{{ scope.row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('提醒时间')" align="center" prop="remindTime" width="130" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.remindTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('触发状态')" align="center" prop="isTrigger" width="130" show-overflow-tooltip>
        <template slot-scope="scope">
          <span style="display: none">{{ scope.row.isTrigger }}</span>
          <!-- 已处理 -->
          <el-tag v-if="scope.row.isTrigger === 'YCL'" type="success">{{ $t(scope.row.isTriggerName) }}</el-tag>
          <!-- 未触发 -->
          <el-tag v-if="scope.row.isTrigger === 'WCF'" type="warning">{{ $t(scope.row.isTriggerName) }}</el-tag>
          <!-- 未处理 -->
          <el-tag v-if="scope.row.isTrigger === 'WCL'" type="danger">{{ $t(scope.row.isTriggerName) }}</el-tag>
          <!-- 失效 -->
          <el-tag v-if="scope.row.isTrigger === 'SX'" type="info">{{ $t(scope.row.isTriggerName) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('触发时间')" align="center" prop="triggerTime" width="130" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.triggerTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('条款内容')" align="center" prop="remark" width="160" show-overflow-tooltip>
        <template #default="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('总结内容')" align="center" prop="summaryContent" width="160" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.summaryContent }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 关键条款对话框 -->
    <el-dialog :title="title" :visible.sync="dealTermsOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealContractTermsForm ref="dealContractTermsForm" />
    </el-dialog>
    <!-- hetong -->
    <el-dialog :visible.sync="infoOpen" :title="$t('流程详情')" custom-class="dialogForm" style=" text-align: left;" :close-on-click-modal="false" :lock-scroll="false">
      <ProcessInfoForm ref="processInfoForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listTermsOverview } from '@/api/fund/overview/termsOverview'
import dealContractTermsForm from '@/views/deal/terms/dealTermsForm.vue'
import ProcessInfoForm from '@/views/act/process/processInfoForm'
import { findFund } from '@/api/fund/fundBase'
export default {
  components: {
    dealContractTermsForm,
    ProcessInfoForm
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 遮罩层
          infoOpen: false,
          // 总条数
          total: 0,
          // 项目关键条款表格数据
          termsOverviewList: [],
          // 弹出层标题
          title: '',
          // 是否显示弹出层
          dealTermsOpen: false,
          // 条款类型字典
          typeOptions: [],
          // 字典标签字典
          istriggernameOptions: [],
            // 查询参数
            queryParams: {
                fundId: undefined,
                dealName: undefined,
                personCharge: undefined,
                associatedName: undefined,
                name: undefined,
                typeName: undefined,
                remindTime: undefined,
                isTriggerName: undefined,
                isTrigger: undefined,
                pageNum: 1,
                pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          },
          rowIndex: '-1',
          OrderIndexArr: [],
          hoverOrderArr: [],
          fundBaseList: []
      }
  },
  computed: {
    groupDealName() { // 获取项目名称
      return new Set(this.termsOverviewList.map(o => o.dealName))
    },
    groupAssName() { // 获取合同名称
      return new Set(this.termsOverviewList.map(o => o.associatedName))
    }
  },
  created() {
      findFund({}).then(res => {
        this.fundBaseList = res.data
      })
      this.getList()
      this.getDicts('key_terms_tyoe').then(response => {
        this.typeOptions = response.data
        this.getDicts('key_terms_type_bf').then(response2 => {
          response2.data && response2.data.forEach(item => {
            this.typeOptions.push(item)
          })
        })
      })
      this.getDicts('key_terms_status').then(response => {
          this.istriggernameOptions = response.data
      })
  },
  methods: {
    /** 查询项目关键条款列表 */
    getList() {
        this.loading = true
        listTermsOverview(this.queryParams).then(response => {
            this.termsOverviewList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 表单重置
    reset() {
        this.form = {
            dealName: undefined,
            remindUserId: undefined,
            formDealId: undefined,
            nickName: undefined,
            contractActId: undefined,
            associatedContract: undefined,
            associatedName: undefined,
            name: undefined,
            type: undefined,
            typeName: undefined,
            remark: undefined,
            remindTime: undefined,
            isTrigger: undefined,
            isTriggerName: undefined,
            triggerTime: undefined,
            updateBy: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.queryParams.isTrigger = undefined
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 查看合同 */
    viewContractProcess(data) {
      if (data !== null && data !== '' && data !== undefined) {
        this.infoOpen = true
        this.title = this.$t('合同信息')
        this.$nextTick(() => {
          // 调用form页面的 init 方法初始化数据
          var proData = {}
          proData.actId = data.contractActId
          this.$refs.processInfoForm.init(proData)
        })
      } else {
        this.$message.error(this.$t('无法查看'))
      }
    },
    /** 查看条款 */
    viewTerms(data) {
      if (data !== null) {
        this.dealTermsOpen = true
        this.title = this.$t('关键条款')
        this.$nextTick(() => {
          // 调用form页面的 init 方法初始化数据
          var row = {}
          row.id = data.id
          row.isSaveShow = ''
          row.formDealId = data.formDealId
          this.$refs.dealContractTermsForm.init(row)
        })
      }
    },
    // 表格合并
    dealGroup(name) {
      return this.termsOverviewList.filter(o => o.dealName === name).length
    },
    assGroup(name) {
      return this.termsOverviewList.filter(o => o.associatedName === name).length
    },
    dealLen(name) {
      const tmp = Array.from(this.groupDealName)
      const index = tmp.indexOf(name)
      let len = 0
      for (let i = 0; i < index; i++) {
        len += this.dealGroup(tmp[i])
      }
      return len
    },
    assLen(name) {
      const tmp = Array.from(this.groupAssName)
      const index = tmp.indexOf(name)
      let len = 0
      for (let i = 0; i < index; i++) {
        len += this.assGroup(tmp[i])
      }
      return len
    },
    spanMethod({ row, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 1) { // 合并项目名称
        const len = this.dealGroup(row.dealName)
        const lenName = this.dealLen(row.dealName)
        if (rowIndex === lenName) {
          return {
            rowspan: len,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/overview/termsOverview/export', this.queryParams)
    }
  }
}
</script>
